#include <QApplication>
#include <QPushButton>
#include <QState>
#include <QStateMachine>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QPushButton button("State Machine");

    // 创建3个状态,并添加到状态机中
    QStateMachine matchine;
    QState *s1 = new QState(&matchine);
    QState *s2 = new QState(&matchine);
    QState *s3 = new QState(&matchine);
    //未按钮部件的geometry属性分配一个值, 当进入该状态时,会设置该值
    s1->assignProperty(&button, "geometry", QRect(100, 100, 100, 50));
    s2->assignProperty(&button, "geometry", QRect(300, 100, 100, 50));
    s3->assignProperty(&button, "geometry", QRect(200, 200, 100, 50));
    //使用按钮信号的单击信号,来完成3个状态的切换
    s1->addTransition(&button, SIGNAL(clicked()), s2);
    s2->addTransition(&button, SIGNAL(clicked()), s3);
    s3->addTransition(&button, SIGNAL(clicked()), s1);
    //设置状态机的初始状态,比启动状态机
    matchine.setInitialState(s1);
    matchine.start();

    button.show();

    return app.exec();
}
